/**********************************************************************************
Program:       chinaweathergadget

License:       GNU/GPL version 3

File:          Form_EditOneCity.cs

Description:   添加、编辑单个城市的弹出窗体

Author:        Guangyu Liu （lgy0417@gmail.com）

Environment:   .NET Framework v2.0 or later version

Usage/Example: README_chs.txt

Last Modified: 2010.08.30

***********************************************************************************/


using System;
using System.Drawing;
using System.Resources;
using System.Reflection;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WeatherInfo;
using PrettifiedControls.PrettifiedButton;
using PrettifiedControls.PrettifiedComboBox;


namespace GUI
{
	public class AddOrEditOneCityForm : Form   // 此窗体在其父窗体的“添加”或“编辑”按钮按下时弹出
	{
		#region 
		private ButtonNew ok;
		private ComboBoxNew province_Combo;
		private ComboBoxNew city_Combo;
		private ComboBoxNew town_Combo;
		private ID_Database idd;
		private AddOrEditOneCityDelegate aoeocd;
		private ResourceManager r_manager;
		private bool isAdding = true;   // 标记是在“添加”还是“编辑”
		private SingleCity cityBeingEdited;
		#endregion
	
		#region constructors
		
		public AddOrEditOneCityForm(ID_Database idd, AddOrEditOneCityDelegate aoeocd)
		{
			// 点“添加”按钮时调用此构造函数
			this.idd = idd;
			this.aoeocd = aoeocd;
			InitializeComponents();
		}
		
		public AddOrEditOneCityForm(ID_Database idd, AddOrEditOneCityDelegate aoeocd, SingleCity cityBeingEdited)
		{
			// 点“编辑”按钮时调用此构造函数
			this.idd = idd;
			this.aoeocd = aoeocd;
			this.cityBeingEdited = cityBeingEdited;
			this.isAdding = false;
			InitializeComponents();
		}
		
		#endregion
		
		private void InitializeComponents()
		{
			r_manager = new ResourceManager("ChinaWeatherGadget", Assembly.GetExecutingAssembly());
		
			#region ComboBox
			
			province_Combo = new ComboBoxNew();
			province_Combo.Width = 75;
			province_Combo.Top = 20;
			province_Combo.Left = 20;
			province_Combo.DropDownStyle = ComboBoxStyle.DropDownList;
			this.Controls.Add(province_Combo);
			province_Combo.SelectedIndexChanged += new System.EventHandler(this.PCombo_Changed);
			
			city_Combo = new ComboBoxNew();
			city_Combo.Width = 75;
			city_Combo.Top = province_Combo.Top;
			city_Combo.Left = province_Combo.Right + 18;
			city_Combo.DropDownStyle = ComboBoxStyle.DropDownList;
			this.Controls.Add(city_Combo);
			city_Combo.SelectedIndexChanged += new System.EventHandler(this.CCombo_Changed);
			
			town_Combo = new ComboBoxNew();
			town_Combo.Width = 75;
			town_Combo.Top = province_Combo.Top;
			town_Combo.Left = city_Combo.Right + 18;
			town_Combo.DropDownStyle = ComboBoxStyle.DropDownList;
			this.Controls.Add(town_Combo);
			
			#endregion
			
			#region button
			ok = new ButtonNew();
			ok.Top = province_Combo.Top - 2;
			ok.Left = town_Combo.Right + 18;
			ok.Height = 25;
			ok.Width = 60;
			ok.Text = "确定";
			ok.Image = (Image)r_manager.GetObject("ok_png");
			ok.ImageWidth = 14;
			ok.TextImageRelation = TextImageRelation.ImageBeforeText;
			this.Controls.Add(ok);
			ok.Click += new System.EventHandler(this.buttonOK_Click);
			#endregion
		
			#region form
			this.StartPosition = FormStartPosition.CenterParent;
			this.MaximizeBox = false;
			this.Opacity = 0.96;
			this.BackColor = Color.FromArgb(224,234,247);
			this.ClientSize = new Size(ok.Right + 20, province_Combo.Bottom + 20);
			this.FormBorderStyle = FormBorderStyle.FixedSingle;
			this.Text = "请选择新城市";
			this.Icon = (Icon)r_manager.GetObject("16x16_icon");
			this.AcceptButton = ok;
			this.ShowInTaskbar = false;
			this.Load += new System.EventHandler(this.Form_Load);
			#endregion
		}
		
		private void PCombo_Changed(object sender, System.EventArgs e)
		{
			city_Combo.Items.Clear();
			city_Combo.Items.AddRange(idd.GetNameRange(province_Combo.SelectedIndex));
			city_Combo.SelectedIndex = 0;
			town_Combo.Items.Clear();
			town_Combo.Items.AddRange(idd.GetNameRange(province_Combo.SelectedIndex, city_Combo.SelectedIndex));
			town_Combo.SelectedIndex = 0;
		}
		
		private void CCombo_Changed(object sender, System.EventArgs e)
		{
			town_Combo.Items.Clear();
			town_Combo.Items.AddRange(idd.GetNameRange(province_Combo.SelectedIndex, city_Combo.SelectedIndex));
			town_Combo.SelectedIndex = 0;
		}
		
		private void Form_Load(object sender, System.EventArgs e)
		{
			province_Combo.Items.AddRange(idd.GetNameRange());
			if (!isAdding)
			{
				province_Combo.SelectedIndex = cityBeingEdited.IndexOfProvince;
				city_Combo.SelectedIndex = cityBeingEdited.IndexOfCity;
				town_Combo.SelectedIndex = cityBeingEdited.IndexOfTown;
			}
			else
				province_Combo.SelectedIndex = 0;
		}
		
		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			// 回调父窗体中的 AddOneCity 函数
			Place newPlace = idd.GetTownPlaceStruct(province_Combo.SelectedIndex, city_Combo.SelectedIndex, town_Combo.SelectedIndex);
			SingleCity newCity = new SingleCity(province_Combo.SelectedIndex, city_Combo.SelectedIndex, town_Combo.SelectedIndex, newPlace.Value, newPlace.Name);
			if (isAdding || (!isAdding && !newCity.Equals(cityBeingEdited)))
				aoeocd(newCity, isAdding);
			this.Close();
		}
	}
}